package cn.good.yan.switch1;

/**
 * 1. switch表达式
 * @author shengren.yan
 * @create 2023-05-28
 */
public class SwitchTest {

    public static void main(String[] args) throws Exception {

        SwitchTest v = new SwitchTest();
        // 之前写法
        v.test1();
        // 新写法
        v.test2();
    }

    // 之前写法
    public void test1() throws Exception {
//        String abc = "";
//        Type type = Type.AQ;
//        switch (type) {
//            case AQ:
//                abc = "a";
//                break;
//            case BQ:
//                abc = "b";
//                break;
//            default:
//                throw new Exception("异常");
//        }
//        System.out.println(abc);
    }

    // 新写法  (switch也有返回值)  yield 可以设置返回值
    public void test2() throws Exception {
        String abc = "";
        Type type = Type.AQ;
        abc = switch (type) {
            case AQ -> "a";
            case BQ, CQ, DQ, EQ -> "b";
            default -> throw new Exception("异常");
//            default -> {
//                yield "7";
//            }
        };
        System.out.println(abc);

        // 可以省略 - break
        switch (type) {
            case AQ -> {
                abc = "2";
                System.out.println("22a");
            }
            case BQ, CQ, DQ, EQ -> {
                abc = "33";
                System.out.println("33b");
            }
            default -> throw new Exception("异常");
        };
        System.out.println(abc);
    }

}

enum Type{
    AQ,BQ,CQ,DQ,EQ
}
